﻿using ExpenseSystem.Common;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Core.QuoteModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using System.Linq;
using System.Web.Http;
using System.Collections.Generic;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Quote")]

    public class QuoteController : ApiController
    {
        readonly SetInhibition setInhibition;
        readonly GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public QuoteController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }

        readonly QuoteFetcher fetcher = new QuoteFetcher();
        readonly QuoteDetailFetcher dFetcher = new QuoteDetailFetcher();
        readonly SalesPointQuoteFetcher spqFetcher = new SalesPointQuoteFetcher();
        readonly TitleQuoteFetcher tqFetcher = new TitleQuoteFetcher();
        readonly RegionQuoteFetcher rqFetcher = new RegionQuoteFetcher();
        readonly QuoteManager manager = QuoteManager.CreateInstance();
        readonly QuoteDetailManager dManager = QuoteDetailManager.CreateInstance();
        readonly SalesPointQuoteManager spManager = SalesPointQuoteManager.CreateInstance();
        readonly TitleQuoteManager tqManager = TitleQuoteManager.CreateInstance();
        readonly RegionQuoteManager rqManager = RegionQuoteManager.CreateInstance();

        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(fetcher, "SAM_QUOTE_LIST", paramModels, getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "SAM_QUOTE_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(QuoteModel data)
        {
            return NormalControllerHelper.Set(fetcher, "SAM_QUOTE_SET", data, manager, setInhibition,getInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(QuoteModel data)
        {
            return NormalControllerHelper.Remove("SAM_QUOTE_DEL", data, manager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("SAM_QUOTE_DEL", id, manager);
        }
        [Route("ListDetails")]
        [HttpGet]
        public ResponseModel ListDetails(long quoteId)
        {
            return NormalControllerHelper.ListDetail(dFetcher, "SAM_QUOTE_LIST", quoteId, getInhibition);
        }
        [Route("GetByProduction")]
        [HttpGet]
        public ResponseModel GetByProduction(long productionId)
        {
            UserProfile userProfile = Authority.GetUserProfile();
            VQuoteDetailFetcher quoteDetailFetcher = new VQuoteDetailFetcher();
            VQuoteDetailModel quoteDetail = quoteDetailFetcher.GetDefault(userProfile.SiteId,userProfile.Region,userProfile.TitleId,userProfile.SalesPoint,productionId,out string message);
            if(quoteDetail==null)
            {
                return new ResponseModel() { Count = 0, Data = null, Message = "找不到有效数据", Result = Definition.RESP_DATA_NOT_FOUND };
            }
            else
            {
                return new ResponseModel() { Count = 1, Data = JsonHelper.ToJson(quoteDetail,new GetInhibition()), Message = "查找成功", Result = Definition.RESP_SUCCESS };
            }
        }
        [Route("ListTitles")]
        [HttpGet]
        public ResponseModel ListTitles(long quoteId)
        {
            return NormalControllerHelper.ListDetail(tqFetcher, "SAM_QUOTE_LIST", quoteId, getInhibition);
        }
        [Route("ListSalesPoints")]
        [HttpGet]
        public ResponseModel ListSalesPoints(long quoteId)
        {
            return NormalControllerHelper.ListDetail(spqFetcher, "SAM_QUOTE_LIST", quoteId, getInhibition);
        }
        [Route("ListRegions")]
        [HttpGet]
        public ResponseModel ListRegions(long quoteId)
        {
            return NormalControllerHelper.ListDetail(rqFetcher, "SAM_QUOTE_LIST", quoteId, getInhibition);
        }
        [Route("SetDetails")]
        [HttpPost]
        public ResponseModel SetDetails([FromBody] RequestModel<QuoteDetailModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            if (requestData.Data == null)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有数据被更新";
                return response;
            }
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].QuoteId != requestData.MasterId)
                {
                    requestData.Data[i].QuoteId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(dFetcher, dFetcher, "SAM_QUOTE_SET", requestData, dManager, setInhibition);
        }

        [Route("SetTitles")]
        [HttpPost]
        public ResponseModel SetTitles([FromBody] RequestModel<TitleQuoteModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            if (requestData.Data == null)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有数据被更新";
                return response;
            }
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].QuoteId != requestData.MasterId)
                {
                    requestData.Data[i].QuoteId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(tqFetcher, tqFetcher, "SAM_QUOTE_SET", requestData, tqManager, setInhibition);
        }
        [Route("SetSalesPoints")]
        [HttpPost]
        public ResponseModel SetSalesPoints([FromBody] RequestModel<SalesPointQuoteModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            if (requestData.Data == null)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有数据被更新";
                return response;
            }
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].QuoteId != requestData.MasterId)
                {
                    requestData.Data[i].QuoteId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(spqFetcher, spqFetcher, "SAM_QUOTE_SET", requestData, spManager, setInhibition);
        }
        [Route("SetRegions")]
        [HttpPost]
        public ResponseModel SetRegions([FromBody] RequestModel<RegionQuoteModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            if (requestData.Data == null)
            {
                response.Result = Definition.RESP_SUCCESS;
                response.Count = 0;
                response.Data = null;
                response.Message = "没有数据被更新";
                return response;
            }
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].QuoteId != requestData.MasterId)
                {
                    requestData.Data[i].QuoteId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(rqFetcher, rqFetcher, "SAM_QUOTE_SET", requestData, rqManager, setInhibition);
        }
        [Route("RemoveWithDetail")]
        [HttpGet]
        public ResponseModel RemoveWithDetail([FromUri]long id)
        {
            bool result = (NormalControllerHelper.RemoveByMainId(spqFetcher, "SAM_QUOTE_DEL", id, spManager).Result == Definition.RESP_SUCCESS);
            if(result)result = (NormalControllerHelper.RemoveByMainId(rqFetcher, "SAM_QUOTE_DEL", id, rqManager).Result == Definition.RESP_SUCCESS);
            if(result)result = (NormalControllerHelper.RemoveByMainId(tqFetcher, "SAM_QUOTE_DEL", id, tqManager).Result == Definition.RESP_SUCCESS);

            if (result)
                return NormalControllerHelper.RemoveWithDetail(dFetcher, "SAM_QUOTE_DEL", id, manager, dManager);
            else
                return new ResponseModel() { Count = 0, Data = "", Message = "操作失败", Result = Definition.RESP_SET_DATA_ERROR };
        }
        [Route("Submit")]
        [HttpGet]
        public ResponseModel Submit([FromUri]long id)
        {
            return NormalControllerHelper.Submit("SAM_QUOTE_SET", id, manager);
        }
        [Route("Recall")]
        [HttpGet]
        public ResponseModel Recall([FromUri]long id)
        {
            return NormalControllerHelper.Recall("SAM_QUOTE_SET", id, manager);
        }
        [Route("Approve")]
        [HttpGet]
        public ResponseModel Approve([FromUri]long id)
        {
            return NormalControllerHelper.Approved( "SAM_QUOTE_APP", id, manager);
        }
        [Route("Revert")]
        [HttpGet]
        public ResponseModel Revert([FromUri]long id)
        {
            return NormalControllerHelper.Revert("SAM_QUOTE_APP", id, manager);
        }
        [Route("Reject")]
        [HttpGet]
        public ResponseModel Reject([FromUri]long id)
        {
            return NormalControllerHelper.Reject("SAM_QUOTE_APP", id, manager);
        }
        [Route("Confirm")]
        [HttpGet]
        public ResponseModel Confirm([FromUri]long id)
        {
            return NormalControllerHelper.Comfirm("SAM_QUOTE_CFM", id, manager);
        }
    }
}
